{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 4 1 7 2 7 6 4 7 5 "
     ]
    }
   ],
   "source": [
    "from random import choice\n",
    "short_list=[1,2,3,4,5,6,7]\n",
    "for i in range(10):\n",
    "    print(choice(short_list),end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\Demo\\PyTest\\python 语言及其应用 学习笔记\n",
      "c:\\ProgramData\\Anaconda3\\python38.zip\n",
      "c:\\ProgramData\\Anaconda3\\DLLs\n",
      "c:\\ProgramData\\Anaconda3\\lib\n",
      "c:\\ProgramData\\Anaconda3\n",
      "\n",
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\n",
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\\locket-0.2.1-py3.8.egg\n",
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\n",
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib\n",
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin\n",
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions\n",
      "C:\\Users\\liangliang\\.ipython\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "for place in sys.path:\n",
    "    print(place)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Hydrogen': 1, 'Helium': 2}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periodic_table={'Hydrogen':1,'Helium':2}\n",
    "periodic_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "{'Hydrogen': 1, 'Helium': 2, 'Carbon': 12}\n"
     ]
    }
   ],
   "source": [
    "carbon=periodic_table.setdefault('Carbon',12)\n",
    "print(carbon)\n",
    "print(periodic_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "{'Hydrogen': 1, 'Helium': 2, 'Carbon': 12}\n"
     ]
    }
   ],
   "source": [
    "Helium=periodic_table.setdefault('Helium',1234)\n",
    "print(Helium)\n",
    "print(periodic_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "defaultdict(<class 'int'>, {'Hydrogen': 1, 'Lead': 0})\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "periodic_table=defaultdict(int)\n",
    "periodic_table['Hydrogen']=1\n",
    "print(periodic_table['Lead'])\n",
    "print(periodic_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bilibili\n",
      "Huh?\n",
      "defaultdict(<function no_idea at 0x0000011F9B3E8B80>, {'B': 'bilibili', 'C': 'Huh?'})\n"
     ]
    }
   ],
   "source": [
    "def no_idea():\n",
    "    return 'Huh?'\n",
    "bestiary=defaultdict(no_idea)\n",
    "bestiary['B']='bilibili'\n",
    "print(bestiary['B'])\n",
    "print(bestiary['C'])\n",
    "print(bestiary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'int'>, {'spam': 3, 'eggs': 1})\n",
      "spam 3\n",
      "eggs 1\n"
     ]
    }
   ],
   "source": [
    "food_counter=defaultdict(int)\n",
    "food_arr=['spam','spam','eggs','spam']\n",
    "for food in ['spam','spam','eggs','spam']:\n",
    "    food_counter[food]+=1\n",
    "print(food_counter)\n",
    "for food,count in food_counter.items():\n",
    "    print(food,count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'spam': 3, 'eggs': 1})"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "breakfast_counter=Counter(food_arr)\n",
    "breakfast_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('spam', 3), ('eggs', 1)]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breakfast_counter.most_common()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('spam', 3)]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breakfast_counter.most_common(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'eggs': 2, 'bacon': 1})"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lunch=['eggs','eggs','bacon']\n",
    "lunch_counter=Counter(lunch)\n",
    "lunch_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'spam': 3, 'eggs': 3, 'bacon': 1})"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breakfast_counter+lunch_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'spam': 3})"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breakfast_counter-lunch_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'eggs': 1})"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breakfast_counter&lunch_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'spam': 3, 'eggs': 2, 'bacon': 1})"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breakfast_counter|lunch_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Nyuk nyuk!'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import OrderedDict\n",
    "quotes=OrderedDict([\n",
    "    ('Moe','A wise guy,huh?'),\n",
    "    ('Larry','Ow!'),\n",
    "    ('Curly','Nyuk nyuk!'),\n",
    "])\n",
    "quotes['Curly']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Moe A wise guy,huh?\n",
      "Larry Ow!\n",
      "Curly Nyuk nyuk!\n"
     ]
    }
   ],
   "source": [
    "for stooge,v in quotes.items():\n",
    "    print(stooge,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def palindrome(word):\n",
    "    from collections import deque\n",
    "    dq= deque(word)\n",
    "    while len(dq) >1:\n",
    "        if dq.popleft() !=dq.pop():\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "palindrome('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "palindrome('abc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "a\n",
      "b\n"
     ]
    }
   ],
   "source": [
    "import itertools\n",
    "for item in itertools.chain([1,2],['a','b']):\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "1\n",
      "2\n",
      "1\n",
      "2\n",
      "1\n",
      "2\n",
      "1\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "for item in itertools.cycle([1,2]):\n",
    "    print(item)\n",
    "    i+=1\n",
    "    if(i>10):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "6\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "for item in itertools.accumulate([1,2,3,4]):\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "6\n",
      "24\n"
     ]
    }
   ],
   "source": [
    "for item in itertools.accumulate([1,2,3,4],lambda x,b:x*b):\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('Moe', 'A wise guy,huh?'),\n",
      "             ('Larry', 'Ow!'),\n",
      "             ('Curly', 'Nyuk nyuk!')])\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "pprint(quotes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'zhangsan': 2, 'lisi': 1}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_arr=['zhangsan','lisi','zhangsan']\n",
    "dict_d=dict()\n",
    "for i in user_arr:\n",
    "    if dict_d.get(i) is None:\n",
    "        dict_d[i]=1\n",
    "    else:\n",
    "        dict_d[i]+=1\n",
    "dict_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'zhangsan': 2, 'lisi': 1}"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_arr=['zhangsan','lisi','zhangsan']\n",
    "dict_d=dict()\n",
    "for i in user_arr:\n",
    "    num=dict_d.get(i,0)\n",
    "    # dict_d[i].setdefault(0)\n",
    "    dict_d[i]=num+1\n",
    "dict_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'zhangsan': 2, 'lisi': 1}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_arr=['zhangsan','lisi','zhangsan']\n",
    "dict_d=dict()\n",
    "for i in user_arr:\n",
    "    dict_d.setdefault(i,0)\n",
    "    # dict_d[i]+=1\n",
    "    dict_d[i] = dict_d[i] + 1\n",
    "dict_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(int, {'zhangsan': 2, 'lisi': 1})"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_arr=['zhangsan','lisi','zhangsan']\n",
    "dict_d=defaultdict(int)\n",
    "for i in user_arr:\n",
    "    dict_d[i]+=1\n",
    "dict_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'zhangsan': 2, 'lisi': 1})"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(user_arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'zhangsan': 2, 'lisi': 1}"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_s=set(user_arr)\n",
    "dict_dd=dict()\n",
    "for i in set_s:\n",
    "    dict_dd[i]=user_arr.count(i)\n",
    "dict_dd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(('zhangsan', '1'), ('zhangsan', '12'), ('lisi', '1'), ('lisi', '12'))"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_arr=['zhangsan','lisi']\n",
    "phone=['1','12']\n",
    "dict_d= ((k,v) for k in user_arr for v in phone )\n",
    "tuple(dict_d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "xiaoming={'user_id':'1234',\n",
    "          'password':'1122'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '2']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# arr=['1','2']\n",
    "arr='1,2'.split(',')\n",
    "arr_num=list()\n",
    "print(arr)\n",
    "for i in arr:\n",
    "    arr_num.append(int(i))\n",
    "\n",
    "arr_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 5]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_num.append(5)\n",
    "arr_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aaa='aabbbaaaa'\n",
    "import re\n",
    "pattern = re.compile(\"\\w\\1\")\n",
    "pattern.findall(aaa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2013, 1, 1]\n",
      "2013 1 1\n",
      "这是年2013这是月1月\n"
     ]
    }
   ],
   "source": [
    "s='2013/1/1'\n",
    "arr=s.split('/')\n",
    "arr_num=[]\n",
    "for i in arr:\n",
    "    arr_num.append(int(i))\n",
    "print(arr_num)\n",
    "year ,mouth,day=arr_num\n",
    "print(year,mouth,day)\n",
    "tuple_mouth=('1月','2月')\n",
    "print(f'这是年{year}这是月{tuple_mouth[mouth-1]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2013 1 1\n",
      "这是年2013这是月1月\n"
     ]
    }
   ],
   "source": [
    "s='2013/1/1'\n",
    "year ,mouth,day=  map(int,s.split('/')) \n",
    "print(year,mouth,day)\n",
    "tuple_mouth=('1月','2月')\n",
    "print(f'这是年{year}这是月{tuple_mouth[mouth-1]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
